/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Service;

import DAO.ActividadDAO;
import DAO.DAOFactory;
import DAO.exceptions.IllegalOrphanException;
import DAO.exceptions.NonexistentEntityException;
import DAO.exceptions.RollbackFailureException;
import Entidad.Actividad;
import VO.ActividadVO;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;

/**
 *
 * @author David
 */
public class ActivityServices {
    public void createActivity(ActividadVO actividadVo, EntityManager em)  {
       ActividadDAO actividadDao = (ActividadDAO)DAOFactory.createDAO(DAOFactory.ACTIVIDAD);
       int size = actividadDao.getActividadCount(em);
       actividadVo.setIdactividad(size+1);
       Actividad actividad = actividadVo.toEntity();
       actividadDao.create(actividad, em);
    }
    public void updateActivity(ActividadVO vo, EntityManager em)  {
    }
    public void deleteActivity(int id, EntityManager em) {
        ActividadDAO actividadDAO = (ActividadDAO)DAOFactory.createDAO(DAOFactory.ACTIVIDAD);
        try {
            actividadDAO.destroy(id, em);
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(ActivityServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(ActivityServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            Logger.getLogger(ActivityServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ActivityServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    public ActividadVO readActivity(int id, EntityManager em) {
        ActividadDAO projectDAO = (ActividadDAO)DAOFactory.createDAO(DAOFactory.ACTIVIDAD);
        Actividad entity = projectDAO.findActividad(id, em);
        if(entity != null) {
           ActividadVO vo =  entity.toVO();
           return vo;
        }
        else {
            return null;
        }
    }

}
